/// 
/// Angular module
/// -------------------------------------------------------------------------------------------------------------------
/// <reference path="_references.ts" />
'use strict';
angular.module('app', ['app.filters', 'app.services', 'app.directives', 'app.controllers', 'ui.bootstrap', 'ngCookies', 'ngRoute', 'ui.select2', 'ngStorage', 'geocoder', 'datePicker', 'underscore', 'angular-gestures'])
       // .constant('_START_REQUEST_', '_START_REQUEST_')  '_START_REQUEST_', '_END_REQUEST_'
      //  .constant('_END_REQUEST_', '_END_REQUEST_')   _START_REQUEST_, _END_REQUEST_
      .config([<any>  '$routeProvider', <any> '$locationProvider', <any> '$httpProvider', function ($routeProvider: any, $locationProvider: any, $httpProvider: any) {
          $routeProvider.
              when('/Teams/:id', {
                  templateUrl: 'scripts/app/teamdetails.view.html', controller: 'teamDetailCtrl',
                  //access: access.user
              }).
              when('/Incidents/:id', {
                  templateUrl: 'scripts/app/incidentdetail.view.html', controller: 'incidentDetailCtrl',
                  //access: access.user
                  //LocationDetailCtrl
              }).
               when('/LocationDetail/:id', {
                   templateUrl: 'scripts/app/locationdetail.view.html', controller: 'locationDetailCtrl',
                   //access: access.use
               }).
               when('/LocationDetail/:id/:id2', {
                   templateUrl: 'scripts/app/locationdetail.view.html', controller: 'locationDetailCtrl',
                   //access: access.use
               }).
               when('/Victims/:id', {
                   templateUrl: 'scripts/app/victims.view.html', controller: 'victimsCtrl',
                   //access: access.use
               }).
                when('#/Victims/:id', {
                    templateUrl: 'scripts/app/victims.view.html', controller: 'victimsCtrl',
                    //access: access.use
                }).
              when('#/LocationDetail/:id', {
                  templateUrl: 'scripts/app/locationdetail.view.html', controller: 'locationDetailCtrl',
                  //access: access.use
              }).
               when('#/LocationDetail/:id/:id2', {
                   templateUrl: 'scripts/app/locationdetail.view.html', controller: 'locationDetailCtrl',
                   //access: access.use
               }).
              when('#/Teams', {
                  templateUrl: 'scripts/app/teams.view.html', controller: 'teamCtrl',
                  //access: access.user
              }).
                when('/Teams', {
                    templateUrl: 'scripts/app/teams.view.html', controller: 'teamCtrl',
                    //access: access.user
                }).
               when('/Incidents', {
                   templateUrl: 'scripts/app/incident.view.html', controller: 'incidentCtrl',
                   //access: access.user
               }).
               when('#/Incidents', {
                   templateUrl: 'scripts/app/incident.view.html', controller: 'incidentCtrl',
                   //access: access.user
               }).
              when('/', {
                  templateUrl: 'scripts/app/home.view.html', controller: 'homeCtrl',
                  //access: access.user
              }).
          when('#/', {
              templateUrl: 'scripts/app/home.view.html', controller: 'homeCtrl',
              //access: access.user
          })
              .when('#/About', {
                  templateUrl: 'scripts/app/about.view.html', controller: 'aboutCtrl',
                  //access: access.user
              })
                .when('/About', {
                    templateUrl: 'scripts/app/about.view.html', controller: 'aboutCtrl',
                    //access: access.user
                })
               .when('/Admin/:id', {
                   templateUrl: 'scripts/app/admin.view.html', controller: 'adminCtrl',
                   //access: access.admin
               })
            .when('#/404', {
                templateUrl: 'scripts/app/404.html',
                //access: access.public
            })
              .otherwise({ redirectTo: 'scripts/app/home.view.html' });

          var $http;
         //var interceptor = [<any> '$location', <any> '$q', <any> '$injector', function ($location: any, $q: any, $injector: any) {
         //     var rootScope;
         //     function success(response) {
         //         // get $http via $injector because of circular dependency problem
         //         $http = $http || $injector.get('$http');
         //         // don't send notification until all requests are complete
         //         if ($http.pendingRequests.length < 1) {
         //             // get $rootScope via $injector because of circular dependency problem
         //             rootScope = rootScope || $injector.get('$rootScope');
         //             // send a notification requests are complete
         //             rootScope.$broadcast(_END_REQUEST_);
         //         }
         //         return response;
         //     }

         //     function error(response) {
         //         // get $http via $injector because of circular dependency problem
         //         $http = $http || $injector.get('$http');
         //         // don't send notification until all requests are complete
         //         if ($http.pendingRequests.length < 1) {
         //             // get $rootScope via $injector because of circular dependency problem
         //             rootScope = rootScope || $injector.get('$rootScope');
         //             // send a notification requests are complete
         //             rootScope.$broadcast(_END_REQUEST_);
         //         }
         //         else if (response.status === 401) {
         //             $location.path('/login');
         //             return $q.reject(response);
         //         }
         //         else {
         //             return $q.reject(response);
         //         }
         //     }

         //     return function (promise) {
         //         // get $rootScope via $injector because of circular dependency problem
         //         rootScope = rootScope || $injector.get('$rootScope');
         //         // send notification a request has started
         //         rootScope.$broadcast(_START_REQUEST_);

         //         return promise.then(success, error);
         //     }
         // }];

      //    $httpProvider.responseInterceptors.push(interceptor);

      }])

    .value('toastr', this.window.toastr)
.run([<any> '$rootScope', <any>'$location', function ($rootScope: any, $location: any) {

    $rootScope.$on("$routeChangeStart", function (event, next, current) {
        $rootScope.error = null;
        //if (!Auth.authorize(next.access)) {
        //    if (Auth.isLoggedIn()) $location.path('/');
        //    else $location.path('/login');
        //}
    });

}]);

//<!--
//app.run(function ($rootScope, $location, AuthenticationService, RoleService, SessionService) {

//  'use strict';

//  // enumerate routes that don't need authentication
//  var routesThatDontRequireAuth =['/login'];
//  var routesForAdmin =['/admin'];

//  // check if current location matches route  
//  var routeClean = function (route) {
//    return _.find(routesThatDontRequireAuth,
//      function (noAuthRoute) {
//        return _.str.startsWith(route, noAuthRoute);
//});
//};

//  // check if current location matches route  
//  var routeAdmin = function (route) {
//    return _.find(routesForAdmin,
//      function (noAuthRoute) {
//        return _.str.startsWith(route, noAuthRoute);
//});
//};

//  $rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams) {
//    // if route requires auth and user is not logged in
//    if (!routeClean($location.url()) && !AuthenticationService.isLoggedIn()) {
//      // redirect back to login
//      ev.preventDefault();
//      $location.path('/login');
//}
//else if (routeAdmin($location.url()) && !RoleService.validateRoleAdmin(SessionService.currentUser)) {
//      // redirect back to login
//      ev.preventDefault();
//      $location.path('/error');
//}
//});
//});

//-- >